﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xml;
using SHomeWorkshop.LunarPresentation.Widegets;

namespace SHomeWorkshop.LunarPresentation.Expressions
{
    /// <summary>
    /// 括弧。注意与括弧式不同——括弧中没有嵌入的子面板，因而也不会内嵌任何下级公式。
    /// </summary>
    public class Brace : Expression
    {
        public Brace(SubPanel rootPanel, SubPanel parentPanel, Editor editor)
            : base(rootPanel, parentPanel, editor)
        {
            bPanel = new BracePanel(this);
            if (parentPanel != null && parentPanel.Direction == Orientation.Horizontal)
            {
                bPanel.MinHeight = 40;
                bPanel.MinWidth = 20;
                bPanel.Width = 20;
                bPanel.Height = double.NaN;
            }
            else
            {
                bPanel.MinHeight = 20;
                bPanel.MinWidth = 40;
                bPanel.Height = 20;
                bPanel.Width = double.NaN;
            }
            bPanel.Foreground = ExForeground;

            bPanel.MouseDown += new System.Windows.Input.MouseButtonEventHandler(aPanel_MouseDown);
            bPanel.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(aPanel_MouseLeftButtonUp);

            bPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
            bPanel.VerticalAlignment = VerticalAlignment.Stretch;
            this.mainBorder.Child = bPanel;
            this.mainBorder.Background = Brushes.Transparent;
            this.HorizontalAlignment = HorizontalAlignment.Stretch;
            this.VerticalAlignment = VerticalAlignment.Stretch;

            this.chineseName = "括弧";
        }

        void aPanel_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 接收拖动来的公式。
            Globals.DropExpression(this);
        }

        void aPanel_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (Editor == null) return;
            Editor.SelectedExpression = this;

            //准备拖动公式。
            Globals.dragSourceExpression = this;
            e.Handled = true;
        }

        private BracePanel bPanel;

        public BracePanel BPanel
        {
            get { return bPanel; }
        }

        public override void UpdateAlignment()
        {
            if (ParentPanel != null)
            {
                if (ParentPanel.Direction == Orientation.Horizontal)
                {
                    if (bPanel != null)
                    {
                        bPanel.MinHeight = 40;
                        bPanel.MinWidth = 20;
                        bPanel.Width = 20;
                        bPanel.Height = double.NaN;
                    }
                    switch (align)
                    {
                        case Alignment.LeftOrTop:
                            {
                                this.VerticalAlignment = VerticalAlignment.Top; break;
                            }
                        case Alignment.Center:
                            {
                                this.VerticalAlignment = VerticalAlignment.Center; break;
                            }
                        case Alignment.RightOrBottom:
                            {
                                this.VerticalAlignment = VerticalAlignment.Bottom; break;
                            }
                        default:
                            {
                                this.VerticalAlignment = VerticalAlignment.Stretch; break;
                                // 水平子面板中的公式，默认两端对齐。
                            }
                    }
                }
                else if (ParentPanel.Direction == Orientation.Vertical)
                {
                    if (bPanel != null)
                    {
                        bPanel.MinWidth = 40;
                        bPanel.MinHeight = 20;
                        bPanel.Height = 20;
                        bPanel.Width = double.NaN;
                    }
                    switch (align)
                    {
                        case Alignment.LeftOrTop:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Left; break;
                            }
                        case Alignment.Center:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Center; break;
                            }
                        case Alignment.RightOrBottom:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Right; break;
                            }
                        default:
                            {
                                this.HorizontalAlignment = HorizontalAlignment.Stretch; break;
                                // 垂直子面板中的公式，默认两端对齐。
                            }
                    }
                }
            }

            if (bPanel != null)
            {
                bPanel.UpdateBrace();
            }
        }

        private BraceLocation braces = BraceLocation.Start;

        public BraceLocation Braces
        {
            get { return braces; }
            set
            {
                braces = value;
                this.XmlData.SetAttribute("Braces", braces.ToString());
                UpdateAlignment();

                if (Editor != null)
                {
                    Editor.IsModified = true;
                }
            }
        }

        public override Brush ExForeground
        {
            get
            {
                return base.ExForeground;
            }
            set
            {
                UpdateExForegroundWithoutXmlChanging(value);
                UpdateExForegroundToXmlData(value);
                Editor.IsModified = true;
            }
        }

        public override void UpdateExForegroundWithoutXmlChanging(Brush value)
        {
            base.UpdateExForegroundWithoutXmlChanging(value);

            base.exForeground = value;
            this.bPanel.Foreground = value;
        }

        protected override void BuildChildren()
        {
            base.BuildChildren();

            if (this.XmlData == null) return;

            try
            {
                XmlAttribute attrArrows = XmlData.GetAttribute("Braces");
                if (attrArrows != null)
                {
                    this.braces = (BraceLocation)Enum.Parse(typeof(BraceLocation), attrArrows.Value);
                }
            }
            catch (Exception ex)
            {
                Dialogs.LunarMsgBox.MsgBoxShow(
                    Globals.appName,
                    "括弧类型值转换异常",
                    "异常信息如下：\r\n" + ex.Message,
                    ex.StackTrace, MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
        }

        public enum BraceLocation { Start, End }

        public override void SetProperty(string propertyName, string value)
        {
            base.SetProperty(propertyName, value);

            switch (propertyName)
            {
                case "Braces":
                    {
                        this.Braces = (BraceLocation)Enum.Parse(typeof(BraceLocation), value);
                        break;
                    }
            }
        }
    }
}
